/**
 * Created by lx on 2017/3/2.
 */
angular.module('model.homepage',[
])
    .config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
        $stateProvider
            //首页
            .state('botMenu.homepage', {
                url: '/homepage',
                templateUrl: 'view/home/homepage.html',
                controller: 'HomepageController'
            })

        ;
    }])

    .controller('HomepageController', ['$scope','$state','MarketService','LoginService',function($scope, $state,MarketService,LoginService){
        MarketService.get().$promise.then(function (data) {
            $scope.markets = data;
            console.log(data)
            getaddr($scope.markets,0);
            var url = window.location.href;
            var page = {
                name: "首页",
                url: url
            };
            LoginService.SavePageList(page);

            function getaddr(list, i)  {
                if (i >= list.length) {
                    return
                }
                var item = list[i];
                regeocoder(item,function (status, result) {
                    console.log(result);
                    console.log(status);
                    $scope.$apply(function () {
                        if (status == 'complete') {
                            if (result.info == 'OK') {
                                item.addr = result.regeocode.addressComponent.city
                                console.log(item.addr);
                            }
                            else {
                                item.addr = "无地址";
                            }
                        }
                        else {
                            console.log("222");
                            item.addr = "无地址";
                        }
                    });
                    //$scope.markets.push(item);
                    getaddr(list, i + 1);
                    //data[i].addr = result.regeocode.addressComponent.city
                    //console.log(data)
                })


            }
            console.log($scope.markets)



        });
        $scope.gomarket = function(marketid){
            $state.go('botMenu.market',{marketid:marketid})
        };
        jssor_1_slider_init();


    }])
;

/*面包屑导航*/
/*Create by jx*/
var myApp = angular.module('myApp', []);
myApp.controller('Ctrl', function($scope){
    $scope.crumbOptions = [
        {"templateurl": 'view/home/homepage.html', "title" : "首页"},
        {"templateurl": 'view/advertisement/advertisement', "title" : "广告投放"},
        {"templateurl": 'view/order/ordercenter', "title" : "订单中心"},
        {"templateurl": 'view/advertisement/advertisementcenter',"title" : "广告管理"},
        {"templateurl": 'view/login/login', "title" : "近期动态"}
    ];
});
myApp.directive("custBreadcrumb", function() {
    return {
        restrict: 'E',
        replace: true,
        scope: {
            options:'@'
        },
        link: function(scope, elem, attrs) {
            var parentNode = elem.parent();
            var crumbString = '<ol class="breadcrumb">';

            angular.forEach(scope.$eval(scope.options), function(item) {
                if (item["href"] != "") {
                    var tempString = '<li><a href="' + item["href"] + '">' + item["title"] + '</a></li>';
                    crumbString += tempString;
                } else {
                    var tempString = '<li class="active">' + item["title"] + '</li>';
                    crumbString += tempString;
                }
            });

            crumbString += "</ol>";
            parentNode.append(crumbString);
        }
    };
});
